home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / mosmllib / List.sml < prev    next >
Encoding:
Text File  |  1996-07-03  |  2.2 KB  |  97 lines  |  [TEXT/R*ch]

  1. (* List -- as of 1995-03-08 *)
  2.  
  3. exception Empty;
  4.  
  5. fun null [] = true
  6.   | null _  = false;
  7.  
  8. fun hd []      = raise Empty
  9.   | hd (x::xr) = x;
  10.  
  11. fun tl []      = raise Empty
  12.   | tl (x::xr) = xr;
  13.  
  14. fun last []      = raise Empty
  15.   | last [x]     = x
  16.   | last (x::xr) = last xr;
  17.  
  18. fun nth (xs, n) =
  19.     let fun h []      _ = raise Subscript
  20.       | h (x::xr) n = if n=0 then x else h xr (n-1)
  21.     in if n<0 then raise Subscript else h xs n end;
  22.  
  23. fun drop (xs, n) =
  24.     let fun h xs      0 = xs
  25.       | h []      n = raise Subscript
  26.       | h (x::xr) n = h xr (n-1)
  27.     in if n<0 then raise Subscript else h xs n end;
  28.  
  29. fun take (xs, n) =
  30.     let fun h xs      0 = []
  31.       | h []      n = raise Subscript
  32.       | h (x::xr) n = x :: h xr (n-1)
  33.     in if n<0 then raise Subscript else h xs n end;
  34.  
  35. fun length xs =
  36.     let fun acc []      k = k
  37.           | acc (x::xr) k = acc xr (k+1)
  38.     in acc xs 0 end;
  39.  
  40. local
  41.   fun revAcc [] ys = ys
  42.     | revAcc (x::xs) ys = revAcc xs (x::ys)
  43. in 
  44.   fun rev xs = revAcc xs []
  45.  
  46.   fun revAppend (xs, ys) = revAcc xs ys
  47. end
  48.  
  49. local
  50.   fun append [] ys = ys
  51.     | append (x::xs) ys = x :: append xs ys
  52. in
  53.   fun xs @ ys = append xs ys
  54. end
  55.  
  56. fun concat []        = []
  57.   | concat (xs::xsr) = xs @ concat xsr;
  58.  
  59. fun app f []      = ()
  60.   | app f (x::xr) = (f x; app f xr);
  61.  
  62. fun map f [] = []
  63.   | map f (x::xs) = f x :: map f xs
  64.  
  65. fun mapPartial f []      = []
  66.   | mapPartial f (x::xr) = case f x of NONE   => mapPartial f xr
  67.                                      | SOME r => r :: mapPartial f xr;
  68.  
  69. fun find p []      = NONE
  70.   | find p (x::xr) = if p x then SOME x else find p xr;
  71.  
  72. fun filter p []      = []
  73.   | filter p (x::xr) = if p x then x :: filter p xr else filter p xr;
  74.  
  75. fun partition p xs =
  76.     let fun h []      are aren't = (rev are, rev aren't)
  77.       | h (x::xr) are aren't = if p x then h xr (x::are) aren't
  78.                           else h xr are      (x::aren't)
  79.     in h xs [] [] end;
  80.  
  81. fun foldr f e []      = e
  82.   | foldr f e (x::xr) = f(x, foldr f e xr);
  83.  
  84. fun foldl f e []      = e
  85.   | foldl f e (x::xr) = foldl f (f(x, e)) xr;
  86.  
  87. fun exists p []      = false
  88.   | exists p (x::xr) = p x orelse exists p xr;
  89.  
  90. fun all p []      = true
  91.   | all p (x::xr) = p x andalso all p xr;
  92.  
  93. fun tabulate (n, f) =
  94.     let fun h i = if i<n then f i :: h (i+1) else []
  95.     in if n<0 then raise Size else h 0 end;
  96.  
  97.